import 'package:flutter/material.dart';
import 'package:my_app/base/foundation/flutter_extension.dart';

import '../../base/utils/jd_log.dart';

class NavigatorUtil {
  NavigatorUtil._();

  /// 返回
  static void goBack(BuildContext context, [Map<String, dynamic>? result]) {
    unFocus();
    jdLog('返回1-------');
    jdLog('返回1-------');

    Navigator.pop(context, result);
  }

  // 可能返回
  static Future<bool> maybePop(BuildContext context, [result]) {
    return Navigator.maybePop(context, result);
  }

  /// 返回
  static void maygoBack(BuildContext context, [Map<String, dynamic>? result]) {
    unFocus();
    jdLog('返回1-------');
    Navigator.pop(context, result);
  }

  static void unFocus() {
    if (FocusManager.instance.primaryFocus?.hasFocus ?? false) {
      FocusManager.instance.primaryFocus?.unfocus();
    }
  }

  static Future<dynamic> nav(BuildContext context, Map data,
      {bool replacement = false}) async {
    String jumpLocation = (data['jump_location'] ??
            data['jump_url'] ??
            data['location']) as String? ??
        '';

    // TDLoggerUtil.dt('YouthNavUtils',
    //     'replacement:$replacement  jumpLocation:$jumpLocation');
    final Uri uri = Uri.parse(jumpLocation);
    final String path = uri.path;
    final params = uri.queryParameters;

    if (path == '/pages/users/user_balance/index') {
      return NavigatorUtil.jumpWebPageByLabel(context, 'activity_recharge');
    }

    /// 路由映射
    final routerContractPageMapping = {
      // '/pages/web_view/index': RouterContractPage.instance.tDWebViewPage,
    };
    final routerContractPage = routerContractPageMapping.get(path);
    if (routerContractPage == null) {
      return Future.error('找不到指定的路由(${uri.path})');
    }
    return routerContractPage.pushContractPage(context, params,
        replacement: replacement);
  }

  static Future<Map<String, dynamic>?> jumpWebPageByLabel(
      BuildContext context, String label) async {
    return null;

    // final h5urlByLabel = await GlobalConfig.getH5UrlByLabel(label);
    // return RouterContractPage.instance.webViewPage.push(context, {
    //   'title': null,
    //   'url': h5urlByLabel.value,
    // });
  }
}
